import mongoose from "mongoose";

// 连接数据库
async function main() {
  try {
    await mongoose.connect(
      "mongodb://express:123456@127.0.0.1:9936/test?authSource=admin&directConnection=true&serverSelectionTimeoutMS=2000"
    );
    console.log("数据库连接成功");
  } catch (err) {
    console.error("数据库连接失败:", err);
  }
}

// 监听连接事件
mongoose.connection.on("connected", async () => {
  console.log("Mongoose 已连接到数据库");
});

mongoose.connection.on("error", (err) => {
  console.error("Mongoose 连接错误:", err);
});

mongoose.connection.on("disconnected", () => {
  console.log("Mongoose 已断开连接");
});

main().catch((err) => console.error(err));

console.log("开始连接");
